package com.dissertation.compoent;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DefaultUserInterceptor implements HandlerInterceptor {

//    String username = "teacher123456";
//    String password = "teacher123456";
//    String username = "admin123456";
//    String password = "admin123456";


    @Override
    public boolean preHandle( HttpServletRequest request,
                              HttpServletResponse response, Object handler ) throws Exception {


//        System.err.println( "设置默认账户" );
//        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken( username, password );
//        Subject subject = SecurityUtils.getSubject();
//        subject.login( usernamePasswordToken );
        System.out.println(request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", request.getHeader("Access-Control-Request-Method"));
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));
        return true;
    }

    @Override
    public void postHandle( HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView ) throws Exception {

    }

    @Override
    public void afterCompletion( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex ) throws Exception {

    }
}
